package com.ruoyi.api.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.api.dto.equipment.EquipmentImageDetailDto;
import com.ruoyi.api.dto.equipment.EquipmentMapStatDto;
import com.ruoyi.api.dto.equipment.EquipmentStatDto;
import com.ruoyi.api.dto.equipment.EquipmentTypeStatDto;
import com.ruoyi.api.dto.equipment.resp.EquipmentMapStatisticsRespDto;
import com.ruoyi.api.dto.equipment.resp.EquipmentRealTimeStatisticsRespDto;
import com.ruoyi.api.dto.equipment.resp.EquipmentStatisticsRespDto;
import com.ruoyi.common.tools.enums.DeviceTypeEnum;
import com.ruoyi.common.tools.enums.ServiceTypeEnum;
import com.ruoyi.common.tools.enums.StatTimeEnum;
import com.ruoyi.common.tools.utils.DateUtils;
import com.ruoyi.erecord.locus.domain.vo.LocusVo;
import com.ruoyi.erecord.locus.service.ILocusService;
import com.ruoyi.erecord.vehicles.service.IVehiclesService;
import com.ruoyi.stat.equipment.domain.EquipmentStat;
import com.ruoyi.stat.equipment.mapper.EquipmentStatMapper;
import com.ruoyi.stat.equipment.service.IEquipmentStatService;

import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Service
public class EquipmentStatService {
	private final IEquipmentStatService iEquipmentStatService;
	private final EquipmentStatMapper baseMapper;
	private final IVehiclesService vehiclesService;
	private final ILocusService locusService;

	public EquipmentStatisticsRespDto getEquipmentStatistics(String type) {
		EquipmentStatisticsRespDto resDto = new EquipmentStatisticsRespDto();
		int day = StatTimeEnum.getDayByTimeType(type);
		String queryDate = DateUtils.getPreDate(day);

		resDto.setAll(this.getEmployeeStatDto(type, queryDate));
		resDto.setServiceTypes(this.getEmployeeTypeStatDtoList(type, queryDate));

		return resDto;
	}

	public EquipmentRealTimeStatisticsRespDto getEquipmentRealTimeStatistics(String date) {
		if (StrUtil.isEmpty(date)) {
			//date = DateUtils.getCurrDate();
            /**
             * 20240315，为了应对领导参观，临时把无法统计出来的数据写固定值
             * begin
             */
            date = "2024-03-14";
            /**
             * end
             */
		}

		EquipmentRealTimeStatisticsRespDto resDto = new EquipmentRealTimeStatisticsRespDto();

		resDto.setAll(this.getEmployeeStatDto(null, date));

		resDto.setServiceTypes(this.getEmployeeTypeStatDtoList(date));

		List<EquipmentImageDetailDto> imgList = this.vehiclesService.getEquipmentImageDetailDtoList();

		// 处理轨迹数据
//		imgList.forEach(i -> {
//			LocusVo locusVo = this.locusService.getLocusVoByOrgId(DeviceTypeEnum.CAR, i.getId());
//			if (null != locusVo) {
//				i.setUsageTime(DateUtils.getDateTime(locusVo.getGpsTime()));
//				i.setUsingTrack(locusVo.getGpsValue());
//			}
//		});

		resDto.setImages(imgList);

		return resDto;
	}

	public EquipmentMapStatisticsRespDto getEquipmentMapStatistics(Integer serviceTypeId, String date) {
		if (StrUtil.isEmpty(date)) {
			date = DateUtils.getCurrDate();
		}
		EquipmentMapStatisticsRespDto resDto = new EquipmentMapStatisticsRespDto();
		resDto.setServiceTypeId(serviceTypeId);

		List<EquipmentMapStatDto> detailList = this.vehiclesService.getEquipmentMapStatDtoList(serviceTypeId, date);
		List<EquipmentMapStatDto> resList = new ArrayList<>(detailList.size());
		// 处理轨迹数据
		detailList.forEach(i -> {
			LocusVo locusVo = this.locusService.getLocusVoByOrgId(DeviceTypeEnum.CAR, i.getId());
			if (null != locusVo) {
				i.setUsageTime(DateUtils.getDateTime(locusVo.getGpsTime()));
				i.setUsingTrack(locusVo.getGpsValue());
				i.setGps(locusVo.getGpsValue());
				resList.add(i);
			}
		});

		resDto.setList(resList);
		return resDto;
	}

	///////////

	private EquipmentStatDto getEmployeeStatDto(String type, String queryDate) {
		LambdaQueryWrapper<EquipmentStat> lqw = Wrappers.lambdaQuery();
		if (null != type) {
			lqw.gt(!StrUtil.isEmpty(queryDate), EquipmentStat::getQueryTime, queryDate);
		} else {
			lqw.eq(!StrUtil.isEmpty(queryDate), EquipmentStat::getQueryTime, queryDate);
		}
		return this.baseMapper.getEmployeeStatDto(lqw);
	}

	private List<EquipmentTypeStatDto> getEmployeeTypeStatDtoList(String type, String queryDate) {
		LambdaQueryWrapper<EquipmentStat> lqw = Wrappers.lambdaQuery();
		lqw.gt(!StrUtil.isEmpty(queryDate), EquipmentStat::getQueryTime, queryDate);
		lqw.groupBy(EquipmentStat::getServiceType);
		List<EquipmentTypeStatDto> list = this.baseMapper.getEmployeeTypeStatDtoList(lqw);
		return this.getList(list);
	}

	private List<EquipmentTypeStatDto> getEmployeeTypeStatDtoList(String queryDate) {
		LambdaQueryWrapper<EquipmentStat> lqw = Wrappers.lambdaQuery();
		lqw.eq(!StrUtil.isEmpty(queryDate), EquipmentStat::getQueryTime, queryDate);
		lqw.groupBy(EquipmentStat::getServiceType);
		List<EquipmentTypeStatDto> list = this.baseMapper.getEmployeeTypeStatDtoList(lqw);
		return this.getList(list);
	}

	private List<EquipmentTypeStatDto> getList(List<EquipmentTypeStatDto> list) {
		List<EquipmentTypeStatDto> srcList = Arrays.stream(ServiceTypeEnum.values()).map(s -> this.buildDefDto(s))
				.collect(Collectors.toList());

		list.forEach(s -> {
			for (EquipmentTypeStatDto dto : srcList) {
				if (s.getServiceTypeId().equals(dto.getServiceTypeId())) {
					srcList.remove(dto);
					break;
				}
			}
		});

		if (!srcList.isEmpty()) {
			list.addAll(srcList);
		}

		return list;
	}


	private EquipmentTypeStatDto buildDefDto(ServiceTypeEnum serviceTypeEnum) {
		EquipmentTypeStatDto dto = new EquipmentTypeStatDto();

		dto.setServiceTypeId(serviceTypeEnum.getCode());
		dto.setServiceTypeName(serviceTypeEnum.getServiceType());

		dto.setCarAvgIntactRate("0");
		dto.setCarAvgUseRate("0");
		dto.setCarCount("0");
		dto.setDeviceAvgIntactRate("0");
		dto.setDeviceAvgUseRate("0");
		dto.setDeviceCount("0");
		dto.setDeviceCount("0");

		return dto;
	}
}
